package com.kkb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RequestMapping("file")
@Controller
public class FileController {
    @RequestMapping("up")
    public String upload(@RequestParam("myFile") MultipartFile file, HttpServletRequest req) throws IOException {
        // 获取文件的原始名称
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        // 获取文件的后缀： .jpg   .png  并随机命名
        String filename = UUID.randomUUID().toString().replace("-","") +originalFilename
                .substring(originalFilename.lastIndexOf("."));
        System.out.println(filename);
        // 文件上传路径设置
        String realPath = req.getServletContext().getRealPath("/files")+"/";
        // 上传操作
        file.transferTo(new File(realPath+filename));
        return "param/success";
    }

    @RequestMapping("hello1")
    public String hello1(){
        return "jsp/fileLoap";
    }
}
